<?php


class PageController extends ControllerBase
{
    public $site = 'http://www.drinkt.org.cn/';
    public function listAction()
    {
        $pageNum = $this->request->getQuery("pageNum", null, 1);
        $size = 50;
        $limit = ($pageNum - 1) * 50;
        $name = $this->request->get("name");
        $where = " 4=4 ";
        if ($name) {
            $where .= " and  p.name like '{%$name%}'";
        }

        $sql = "select p.id, p.title,p.name,p.img_id,p.status,p.type,p.template_id,p.create_at,t.title as tname,t.path as tpath from page_info p left join template t on p.template_id = t.id where {$where} order by p.create_at desc limit {$limit},{$size} ";

        $list = $this->db->fetchAll($sql);
        $total = PageInfo::count($where);
        $allPageNum = ceil($total / $size);
        $data= [];
        foreach ($list as $key=>$vo){
            $data[$key] = $vo;
            if(!$vo['img_id']){
                $data[$key]['img_info']=[];
            }else{
                $data[$key]['img_info']=ImgInfo::find("id in ({$vo['img_id']}) and status=1");
            }

            if($vo['tpath']){
                $data[$key]['path']=$this->site.$vo['tpath'];
            }else{
                $data[$key]['path']='';
            }

        }
        $this->exitMessage(true, '', ["data" => $data, "pageInfo" => ["size" => $size, "current" => $pageNum, "allPageNum" => $allPageNum, "total" => $total]]);
    }

    public function saveAction(){
        $title = $this->request->get("title");
        $name = $this->request->get("name");
        $img_id = $this->request->get("img_id");
        $template_id = $this->request->get("template_id");
        $type = $this->request->get("type");
        if(!$name){
            $this->exitMessage(false,"缺少必要参数");
        }
        $page = New PageInfo();
        $page->title = $title;
        $page->name = $name;
        $page->img_id = $img_id;
        $page->template_id = $template_id;
        $page->type = $type;
        $page->status = 1;
        $page->create_at = date("Y-m-d H:i:s");
        $res = $page->save();
        if(!$res){
            $this->exitMessage(false,"保存失败");
        }
        $this->exitMessage(true,"保存成功");
    }

    public function editAction(){
        $id = $this->request->get("id");
        if(!$id){
            $this->exitMessage(false,"缺少必要参数");
        }
        $name = $this->request->get("name");
        $title = $this->request->get("title");
        $img_id = $this->request->get("img_id");
        $status = $this->request->get("status");
        $template_id = $this->request->get("template_id");
        $type = $this->request->get("type");
        $page = PageInfo::findFirst($id);
        $page->title = $title;
        $page->name = $name;
        $page->img_id = $img_id;
        $page->img_id = $img_id;
        $page->template_id = $template_id;
        $page->type = $type;
        $page->update_at = date("Y-m-d H:i:s");
        $res = $page->save();
        if(!$res){
            $this->exitMessage(false,"保存失败");
        }
        $this->exitMessage(true,"保存成功");
    }

    public function delAction(){
        $id = $this->request->get("id");
        if(!$id){
            $this->exitMessage(false,"缺少必要参数");
        }
        $page = PageInfo::findFirst($id);
        $res = $page->delete();
        if(!$res){
            $this->exitMessage(false,"删除失败");
        }
        $this->exitMessage(true,"删除成功");
    }

    public function getImgAction()
    {
        $page_id = $this->request->get("page_id");
        if(!$page_id){
            $this->exitMessage(false,"缺少必要参数");
        }
        $info = PageInfo::findFirst($page_id);

        $img_list = ImgInfo::find("id in ({$info->img_id})");
        $data = [] ;
        foreach ($img_list as $key=>$vo){
            $data[$key]=$vo->toArray();
            $data[$key]['img_path']=$vo->oss_path;
        }
        $list = $info->toArray();
        $list['img_list'] = $data;
        $this->exitMessage(true,"",$list);

    }
}